package com.example.redis.application;

import com.example.redis.autoregister.config.RedisSerializerBuilderConfig;
import com.example.redis.autoregister.config.RedisTemplateConfig;
import com.example.redis.autoregister.support.BeanCacheKeyRedisSerializerFactory;
import com.example.redis.autoregister.support.Jackson2JsonRedisSerializerFactory;
import com.example.redis.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserApplication2 {
    @RedisTemplateConfig(
            keySerializer = @RedisSerializerBuilderConfig(builder = BeanCacheKeyRedisSerializerFactory.class) ,
            valueSerializer =  @RedisSerializerBuilderConfig(builder = Jackson2JsonRedisSerializerFactory.class))
    private RedisTemplate<Long, User> userRedisTemplate;

    public void save(User user){
        // 保存到 redis 中
        this.userRedisTemplate.opsForValue().set(user.getId(), user);
    }

    public User getById(Long user){
        // 从 redis 中获取 user
        return this.userRedisTemplate.opsForValue().get(user);
    }
}
